OpenRoads Designer CONNECT Edition SDK Help

Browse all cells from DGN

The below code snippet's shows how to browse cells from DGN. There are two ways.

1. Browse all cells using Bentley.Interop.MicroStationDGN library


//Required References
using System;
using System.Collections.Generic;
using System.Diagnostics;
using Bentley.Interop.MicroStationDGN;

 public List<CellElement> BrowseAllCellsFromDgn()
        {

            List<CellElement> cellsList = new List<CellElement>();
            //Get all graphical elements cache from active model reference
            Bentley.Interop.MicroStationDGN.ElementCache elementCache = Bentley.MstnPlatformNET.InteropServices.Utilities.ComApp.ActiveModelReference.GraphicalElementCache;

            //Scan to get all elements  
            ElementEnumerator elementEnumerator = elementCache.Scan();
            try
            {
                while (elementEnumerator.MoveNext())
                {
                    //Get current element and check if it is of type cell
                    Bentley.Interop.MicroStationDGN.Element currentElement = elementEnumerator.Current;
                    if (currentElement.IsCellElement())
                    {
                        Bentley.Interop.MicroStationDGN.CellElement cellElement = (Bentley.Interop.MicroStationDGN.CellElement)currentElement;
                        string name = cellElement.Name;
                        cellsList.Add(cellElement);
                    }
                }
            }
            catch (Exception ex)
            {
                Trace.WriteLine(ex.Message);
            }
            return cellsList;
        }

2. Using Bentley.DgnPlatformNET.Elements library


//Required References
using System;
using System.Collections.Generic;
using System.Diagnostics;
using Bentley.DgnPlatformNET;
using Bentley.MstnPlatformNET;
using Bentley.DgnPlatformNET.Elements;

 public List<CellHeaderElement> BrowseAllCellsFromDgn()
        { 
            List<CellHeaderElement> cells = new List<CellHeaderElement>();
            try
            {
                //Get active DGN
                DgnModelRef dgnModelRef = Session.Instance.GetActiveDgnModelRef();
                DgnFile dgnFile = dgnModelRef.GetDgnFile();

                //Get all models 
                ModelIndexCollection modelIndexCollection = dgnFile.GetModelIndexCollection();
                foreach (ModelIndex modelIndex in modelIndexCollection)
                {
                    DgnModel dgnModel = dgnFile.LoadRootModelById(out StatusInt status, modelIndex.Id);

                    //Iterate each model to get graphics element
                    ModelElementsCollection modelElementsCollection = dgnModel.GetGraphicElements();
                    foreach (Bentley.DgnPlatformNET.Elements.Element element in modelElementsCollection)
                    {
                        //Check for cell type elements
                        if (element.TypeName == "Cell")
                        {
                            CellHeaderElement cellHeaderElement = (CellHeaderElement)element;
                            cells.Add(cellHeaderElement);
                        }
                    }
                }
            }
            catch (Exception ex)
            {
                Trace.WriteLine(ex.Message);
            }
            return cells;
        }